//
//  ClubNormalSettingViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit

class ClubNormalSettingViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("通用设置").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubNormalSettingCell.self,ClubNormalCacheCell.self,ClubNormalDelCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        return tableView
    }()
    
    private var settingData: [[ClubSettingModel]] = [
        [.update, .mode, .personal],
        [.policy, .terms]
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(.safe_bottom)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    

}

extension ClubNormalSettingViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        settingData.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubNormalCacheCell", for: indexPath) as! ClubNormalCacheCell
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubNormalSettingCell", for: indexPath) as! ClubNormalSettingCell
        cell.models = settingData[indexPath.section]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 1 || indexPath.section == 2{

            return 74.0.Scale
        }
        let count = settingData[indexPath.section].count
        return Double(count) * 64.0.Scale + 16.0.Scale
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.section == 2 {
            
            if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                let urlString = RaeRequestParameterConfig.shared.baseUrlStr + "ct=zhanwu&ac=userdelete&uid=" + uid + "&t=" + RaeRequestParameterConfig.shared.getTStr() + "&sign=" + RaeRequestParameterConfig.shared.encrySign()
                let webView = ClubWebViewController()
                webView.urlString = urlString
                webView.titleName = "账号注销"
                self.navigationController?.pushViewController(webView, animated: true)
            }
            
        }
    }
    
    
}
